<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="/static/images/favicon.ico" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索结果 - 业天AI网</title>
    <meta name="description" content="业天AI网是一家专业从事发布人工智能、大模型、AI技术、自然语言处理、计算机视觉、机器人、机器学习、深度学习、神经网络、强化学习等领域的最新最前沿的技术和资讯的公司。">
    <meta name="keywords" content="业天AI网,AI新闻,AI资讯,前沿AI,大模型,llm,transform,人工智能信息服务,人工智能优质文章,人工智能媒体,人工智能产业服务,机器智能,机器学习,深度学习,神经网络,强化学习,计算机视觉,自然语言处理,机器人,DeepSeek,large language model,大语言模型,自然语言处理,AI聊天机器人,代码开发,创意写作,AI助手,大模型公司,人工智能">
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* 全局样式 */
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f8f9fa;
        }
        
        /* 导航栏样式 */
        .navbar {
            background-color: #ffffff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }
        
        /* 搜索结果样式 */
        .search-result-container {
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 30px;
        }
        
        /* 新闻条目样式 - 条状显示 */
        .news-item {
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            padding: 15px;
            margin-bottom: 15px;
        }
        
        .news-item:hover {
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
        }
        
        .news-img {
            width: 120px;
            height: 80px;
            object-fit: cover;
            border-radius: 5px;
            margin-right: 15px;
            flex-shrink: 0;
        }
        
        .news-content {
            flex: 1;
            min-width: 0;
        }
        
        .news-title {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 8px;
            color: #333;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
        
        .news-date {
            font-size: 12px;
            color: #999;
        }
        
        /* 响应式调整 */
        @media (max-width: 576px) {
            .news-item {
                padding: 4px;
            }
            
            .news-img {
                width: 100px;
                height: 70px;
                margin-right: 10px;
            }
            
            .news-title {
                font-size: 18px;
            }
        }
        
        /* 加载更多按钮样式 */
        .load-more {
            text-align: center;
            margin: 30px 0;
        }
        
        .load-more-btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 30px;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        
        .load-more-btn:hover {
            background-color: #0056b3;
        }
        
        /* 无结果提示样式 */
        .no-result {
            text-align: center;
            padding: 50px 0;
            color: #999;
        }
        
        /* 加载中动画 */
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        
        .loading::after {
            content: '';
            width: 20px;
            height: 20px;
            border: 2px solid #f3f3f3;
            border-top: 2px solid #007bff;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            display: inline-block;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light">
        <div class="container">
            <a class="navbar-brand" href="/index">
                <img src="/static/images/logo.jpg" alt="业天AI网" height="30">
                <span class="ml-2 font-weight-bold">业天AI网</span>
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
                <!-- 搜索功能模块 -->
                <form class="form-inline mr-4 my-2 my-lg-0" id="searchForm" style="display: flex; align-items: center; white-space: nowrap;">
                    <input class="form-control mr-sm-2" type="search" name="keyword" placeholder="输入关键字" aria-label="Search" style="flex-shrink: 1;" value="{{ keyword }}" id="searchKeyword">
                    <button class="btn btn-outline-success my-2 my-sm-0" type="button" onclick="validateSearch()" style="flex-shrink: 0;">搜索</button>
                </form>
                
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/index">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/login">登录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/register">注册</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <div class="container mt-5">
        <div class="row">
            <!-- 主内容区域 -->
            <div class="col-md-8">
                <div class="search-result-container">
                    <h2>搜索结果："{{ keyword }}"</h2>
                    <div id="news-list" class="news-list">
                        {% if news %}
                            {% for item in news %}
                            <div class="news-item">
                                <img src="{{ item.face_img }}" alt="{{ item.title }}" class="news-img">
                                <div class="news-content">
                                    <a href="/news/{{ item.id }}" class="news-title" title="{{ item.title }}" target="_blank">{{ item.title }}</a>
                                    <div class="news-date">{{ item.publish_at }}</div>
                                </div>
                            </div>
                            {% endfor %}
                        {% else %}
                            <div class="no-result">
                                <h3>未找到相关新闻</h3>
                                <p>请尝试其他关键词</p>
                            </div>
                        {% endif %}
                    </div>
                    
                    <!-- 加载更多按钮 -->
                    {% if has_more %}
                    <div class="load-more">
                        <button class="load-more-btn" onclick="loadMore()">加载更多</button>
                    </div>
                    {% endif %}
                    
                    <!-- 加载中动画 -->
                    <div class="loading" id="loading"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- JavaScript -->
    <!--
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.min.js"></script>
    -->
    <script src="/static/js/jquery-3.5.1.slim.min.js"></script>
    <script src="/static/js/popper.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
    <script>
        let currentPage = {{ page }};
        const keyword = "{{ keyword }}";
        
        function loadMore() {
            currentPage++;
            const loading = document.getElementById('loading');
            const loadMoreBtn = document.querySelector('.load-more-btn');
            
            // 显示加载中动画，隐藏按钮
            loading.style.display = 'block';
            if (loadMoreBtn) loadMoreBtn.style.display = 'none';
            
            // 发送请求加载更多数据
            fetch(`/news/search?keyword=${encodeURIComponent(keyword)}&page=${currentPage}&format=json`)
                .then(response => response.json())
                .then(data => {
                    const newsList = document.getElementById('news-list');
                    
                    // 隐藏加载中动画
                    loading.style.display = 'none';
                    
                    if (data.news.length > 0) {
                        // 添加新的新闻项
                        data.news.forEach(item => {
                            const newsItem = document.createElement('div');
                            newsItem.className = 'news-item';
                            newsItem.innerHTML = `
                                <img src="${item.face_img}" alt="${item.title}" class="news-img">
                                <div class="news-content">
                                    <a href="/news/${item.id}" class="news-title" title="${item.title}" target="_blank">${item.title}</a>
                                    <div class="news-date">${item.publish_at}</div>
                                </div>
                            `;
                            newsList.appendChild(newsItem);
                        });
                        
                        // 如果还有更多数据，显示加载更多按钮
                        if (data.has_more) {
                            if (loadMoreBtn) loadMoreBtn.style.display = 'inline-block';
                        } else {
                            // 如果没有更多数据，移除加载更多按钮
                            const loadMoreDiv = document.querySelector('.load-more');
                            if (loadMoreDiv) loadMoreDiv.remove();
                        }
                    } else {
                        // 如果没有更多数据，移除加载更多按钮
                        const loadMoreDiv = document.querySelector('.load-more');
                        if (loadMoreDiv) loadMoreDiv.remove();
                    }
                })
                .catch(error => {
                    console.error('加载更多数据时出错:', error);
                    loading.style.display = 'none';
                    if (loadMoreBtn) loadMoreBtn.style.display = 'inline-block';
                });
        }
    </script>

    <!-- 空搜索验证模态框 -->
    <div class="modal fade" id="emptySearchModal" tabindex="-1" role="dialog" aria-labelledby="emptySearchModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="emptySearchModalLabel">提示</h5>
            <button type="button" class="close" id="closeModalBtn" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            请输入关键字
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" id="confirmModalBtn">确定</button>
          </div>
        </div>
      </div>
    </div>

    <script>
      function validateSearch() {
        const keywordInput = document.getElementById('searchKeyword');
        const keyword = keywordInput.value.trim();
        
        if (keyword === '') {
          // 显示提示模态框
          $('#emptySearchModal').modal('show');
        } else {
          // 表单提交到搜索结果页面
          const searchForm = document.getElementById('searchForm');
          searchForm.action = '/news/search';
          searchForm.method = 'GET';
          searchForm.submit();
        }
      }
      
      function focusSearchInput() {
        // 当模态框关闭后，将焦点定位到搜索输入框
        setTimeout(() => {
          document.getElementById('searchKeyword').focus();
        }, 300);
      }
      
      // 为关闭按钮绑定事件
      document.getElementById('closeModalBtn').addEventListener('click', function() {
        $('#emptySearchModal').modal('hide');
        focusSearchInput();
      });
      
      // 为确认按钮绑定事件
      document.getElementById('confirmModalBtn').addEventListener('click', function() {
        $('#emptySearchModal').modal('hide');
        focusSearchInput();
      });
      
      // 监听模态框的隐藏事件，确保焦点正确设置
      $('#emptySearchModal').on('hidden.bs.modal', function () {
        focusSearchInput();
      });
      
      // 监听背景点击关闭事件
      $('#emptySearchModal').on('click', function (e) {
        if (e.target === this) {
          $(this).modal('hide');
          focusSearchInput();
        }
      });
    </script>
</body>
</html>